﻿using System;
using System.Collections.Generic;
using System.Text;
using TianShu.DependencyInjection;
using TianShu.MultiTenancy;
using TianShu.Users;

namespace TianShu.SqlSugarCore
{
    public static class SqlSugarDataFilter
    {
        public static string GetDataFilter(this Type entityType, ICurrentTenant currentTenant,string prix="")
        {
            string whereCondition = "1=1";
            if (typeof(ISoftDelete).IsAssignableFrom(entityType))
            {
                whereCondition += $"&& {(prix.IsNullOrEmpty() ? "" : $"{prix}.")}{nameof(ISoftDelete.IsDeleted)}=0";
            }
            if (typeof(IMultiTenant).IsAssignableFrom(entityType) && currentTenant.Id.HasValue)
            {
                whereCondition += $"&& {(prix.IsNullOrEmpty() ? "" : $"{prix}.")}{nameof(IMultiTenant.TenantId)}='{currentTenant.Id}'";
            }
            return whereCondition;
        }
    }
}
